package meihua.stopcarapp.ui;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.Bind;
import meihua.mylibrary.base.BaseActivity;
import meihua.mylibrary.baseapp.AppConfig;
import meihua.mylibrary.commonutils.RxCountDown;
import meihua.stopcarapp.R;
import meihua.stopcarapp.app.UserConfig;
import meihua.stopcarapp.bean.result.UserResult;
import meihua.stopcarapp.model.SplashModel;
import meihua.stopcarapp.presenter.SplashPresenter;
import meihua.stopcarapp.presenter.contract.SplashContract;
import meihua.stopcarapp.utils.ToastUtil;
import rx.Subscriber;
import rx.functions.Action0;

/**
 * Name: LGZ
 * Time: 2017/5/10.
 * Dec:
 */

public class SplashActivity extends BaseActivity<SplashPresenter,SplashModel> implements SplashContract.View {

    @Bind(R.id.iv_logo)
    ImageView ivLogo;
    @Bind(R.id.tv_name)
    TextView tvName;

    String login_status;
    String user_id;
    String user_phone;
    boolean ischeck = false;
    boolean isgetuser = false;
    boolean isLogin = true;

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this,mModel);
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        //起始页倒计时
        RxCountDown.countdown(20)
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        //开始
                        Log.d("ligaozeng","call()");
                        //拿取本地存储的登录状态和用户唯一标识
                        SharedPreferences sharedPreferences=getSharedPreferences("config",0);
                        login_status = sharedPreferences.getString("login_status","1");
                        user_id = sharedPreferences.getString("user_id","");
                        user_phone = sharedPreferences.getString("user_phone","");
                        //登陆状态
                        if (login_status.equals(AppConfig.USER_LOGIN)){
                            ischeck = true;
                            mPresenter.sendGetUser(user_id,user_phone);
                        }
                    }
                })
                .subscribe(new Subscriber<Integer>() {
                    @Override
                    public void onCompleted() {
                        //结束
                        if (isLogin) {
                            MainActivity.startAction(SplashActivity.this);
                            SplashActivity.this.finish();
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                    }
                    @Override
                    public void onNext(Integer integer) {
                        if (isgetuser  && isLogin){
                            isLogin = false;
                            MainActivity.startAction(SplashActivity.this);
                            SplashActivity.this.finish();
                        }
                    }
                });

    }

    @Override
    public void returnUserInfo(UserResult userEntity) {
        isgetuser = true;
        UserConfig.USER_ENTITY = userEntity;
        ToastUtil.showShort(SplashActivity.this,userEntity.getUser_phone());
    }

    @Override
    public void showLoading(String title) {

    }

    @Override
    public void stopLoading() {

    }

    @Override
    public void showErrorTip(String msg) {

    }

}
